package program1;
import java.util.*;
import java.io.*;
public class MyHashMay {//与哈希类相关的代码
	static Map<String,String> hm = new HashMap<>();		
	static HashMap<String, LinkedList<String>> map = new HashMap<>();	
	static HashMap<String,HashMap<String,String>> hmpro = new HashMap<>();
		
	public static void set(String key,String value)//存储k-v的函数
	{		
		hm.put(key,value);
		LinkedList<String> list = new LinkedList<>();
		map.put(key,list);
	}
	
	public static String get(String key)//根据key查找value
	{
		String value = (String)hm.get(key);
		if(value==null)
			value ="null";
		return value;
	}
	
	public static String del(String key)//根据key删除value
	{
		String value = (String)hm.remove(key);
		return value;
	}
	
	public static void pt(String key,int num)//打印，测试用
	{
		LinkedList<String> list = map.get(key);
		String data = list.get(num);
		System.out.println(data);
			
	}
	
	public static void lpush(String key,String data)//直接放一个数据在左端
	{
		LinkedList<String> list = map.get(key);
		if(list!=null)
			list.addFirst(data);
	}
	public static void rpush(String key,String data)//直接放一个数据在右端
	{
		LinkedList<String> list = map.get(key);
		if(list!=null)
			list.addLast(data);
	}
	
	public static String lpop(String key)//获取key最左端的数据并删除
	{
		LinkedList<String> list = map.get(key);
		String data = null;
		if(list!=null)
			data = list.removeFirst();
		return data;
	}
	
	public static String rpop(String key)//获取key最右端的数据并删除
	{
		LinkedList<String> list = map.get(key);
		String data = null;
		if(list!=null)
			data = list.removeLast();
		return data;
	}
	
	public static int len(String key)// 获取 key 存储数据的个数
	{
		int num=0;
		LinkedList<String> list = map.get(key);
		if(list!=null)
		{
			for (String str : list)
				num++;
		}
		return num;
	}
	
	public static void ldel(String key)//删除key 所有的数据
	{
		LinkedList<String> list = map.get(key);
		if(list!=null)
		{
			Iterator<String> iterator = list.iterator();
	        while (iterator.hasNext()) 
	        {
	            iterator.next();
	            iterator.remove();
	        }
		}
	}
	
	public static String[] range(String key,int start,int end)//将key 对应 start 到 end 位置的数据全部返回
	{
		LinkedList<String> list = map.get(key);
		String[] data = null;
		if(list!=null && start>=0 && end>=0 && start<list.size() &&  end<list.size())//判断输入的数据是否合法
		{
			data = new String[end-start+1];//决定数组大小
			for(int i=start;i<end+1;i++)//写入数据
			{
				data[i-start] = list.get(i);
			}
		}
		else				
			 data = new String[]{"输入的数据错误"};
		return data;
	}	
	
	public static void hset(String key,String field,String value)//存储key对应的键值对数据
	{
		
		hmpro.put(key,new HashMap());
		HashMap<String,String> inhm = hmpro.get(key);
		inhm.put(field, value);
	}
	
	public static String hget(String key,String field)//获取key中field字段的value值
	{
		
		HashMap<String,String> inhm = hmpro.get(key);
		String value = inhm.get(field);
		return value;
	}
	
	public static void hdel(String key,String field)//删除key中field字段及其value值
	{
		
		HashMap<String,String> inhm = hmpro.get(key);
		inhm.remove(field);
		
	}
	
	public static void hdel(String key)// 删除key中所有数据
	{
		hmpro.remove(key);
	}
	public static String[] help()//获取全部命令
	{
		String arr[]= {"set [Key] [Value]","get [Key]","del [Key]","lpush [key] [value]",
					   "rpush [key] [value]","range [key] [start] [end]","len [key]","lpop [key]",
					   "rpop [key]","ldel [key]","hset [key] [field] [value]","hget [key] [field]",
					   "hdel [key] [field]","hdel [key]","ping"};
		return arr;
	}
	
	public static String help(String command)//获取command
	{
		String method ;
		switch(command)
		{
			case "set":
				method = "set [Key] [Value]";
				break;
			case "get":
				method = "get [Key]";
				break;
			case "del":
				method = "del [Key]";
				break;
			case "lpush":
				method ="lpush [key] [value]";
				break;
			case "rpush": 
				method ="rpush [key] [value]";
				break;
			case "range": 
				method ="range [key] [start] [end]";
				break;
			case "len": 
				method ="len [key]";
				break;
			case "lpop": 
				method ="lpop [key]";
				break;
			case "rpop": 
				method ="rpop [key]";
				break;
			case "ldel": 
				method ="ldel [key]";	
				break;
			case "hset":
				method = "hset [key] [field] [value]";
				break;
			case "hget":
				method = "hget [key] [field]";
				break;
			case "hdel":
				method = "hdel [key] [field]\nhdel [key]";
				break;
			default:
				method = "查找不到该命令";
		}
		return method;
	}
}












